package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.facade.AccLoanYearTotalService;
import com.fdb.efp.report.service.facade.AccLoanYearTotalTempService;
import com.fdb.efp.report.service.facade.RptAccLoanYearTotalService;
import com.fdb.efp.report.service.vo.AccLoanYearTotalTempVo;
import com.fdb.efp.report.service.vo.RptAccLoanYearTotalTempVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 需求编号:2020D1004
 * 问题编号:
 * 开发人员: wengqingw
 * 创建日期:2020/7/9
 * 功能描述:
 * 修改日期:2020/7/9
 * 修改描述:
 */
@Service("accLoanYearTotalService")
public class AccLoanYearTotalServiceImpl implements AccLoanYearTotalService, FrameworkService {

    private Logger logger = LoggerFactory.getLogger(AccLoanYearTotalServiceImpl.class);
    private final static String channelCodeMs = "04";
    private final static String channelCodeBd = "05";
    private final static String channelMameMs = "马上";
    private final static String channelMameBd = "度小满";
    private final int startTotalAmount = 0;
    private final int fiveTHSTotalAmount = 5000;
    private final int tenTHSTotalAmount = 10000;
    private final int fiftyTHSTotalAmount = 50000;
    private final int oneHundredTHSTotalAmount = 100000;
    private final int oneHundredAndFiftyTHSTotalAmount = 150000;
    private final int twoHundredTHSTotalAmount = 200000;
    private final int twoHundredAndFiftyTHSTotalAmount = 250000;
    private final int threeHundredTHSTotalAmount = 300000;

    @Autowired
    @Qualifier("accLoanYearTotalTempService")
    private AccLoanYearTotalTempService accLoanYearTotalTempService;

    @Autowired
    @Qualifier("rptAccLoanYearTotalService")
    private RptAccLoanYearTotalService rptAccLoanYearTotalService;

    @Override
    public boolean accLoanYearTotal(){
        boolean result = true;
        logger.info("统计用户当年累计放款额度开始");
        try {
            /**
             * 先从acc_loan表中查询回加工好的数据
             */
            logger.info("======>统计用户当年累计放款额度(马上金融)开始");
            //1.1单户借据用信不大于5000的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs1 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, startTotalAmount, fiveTHSTotalAmount);
            //2.1单户借据用信大于5000小于1万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs2 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, fiveTHSTotalAmount, tenTHSTotalAmount);
            //3.1单户借据用信大于1万小于5万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs3 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, tenTHSTotalAmount, fiftyTHSTotalAmount);
            //4.1单户借据用信大于5万小于10万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs4 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, fiftyTHSTotalAmount, oneHundredTHSTotalAmount);
            //5.1单户借据用信大于10万小于15万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs5 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, oneHundredTHSTotalAmount, oneHundredAndFiftyTHSTotalAmount);
            //6.1单户借据用信大于15万小于20万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs6 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, oneHundredAndFiftyTHSTotalAmount, twoHundredTHSTotalAmount);
            //7.1单户借据用信大于20万小于25万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs7 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, twoHundredTHSTotalAmount, twoHundredAndFiftyTHSTotalAmount);
            //8.1单户借据用信大于25万小于30万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalMs8 = accLoanYearTotalTempService.getAccLoanYearTotal(channelCodeMs, twoHundredAndFiftyTHSTotalAmount, threeHundredTHSTotalAmount);
            logger.info("======>统计用户当年累计放款额度(马上金融)结束");
            RptAccLoanYearTotalTempVO rptAccLoanYearTotalTempVO = new RptAccLoanYearTotalTempVO();
            rptAccLoanYearTotalTempVO.setChannelCode(channelCodeMs);
            rptAccLoanYearTotalTempVO.setChannelName(channelMameMs);
            rptAccLoanYearTotalTempVO.setFiveTHSTotalAmount(accLoanYearTotalMs1.getTotalAmount());
            rptAccLoanYearTotalTempVO.setTenTHSTotalAmount(accLoanYearTotalMs2.getTotalAmount());
            rptAccLoanYearTotalTempVO.setFiftyTHSTotalAmount(accLoanYearTotalMs3.getTotalAmount());
            rptAccLoanYearTotalTempVO.setOneHundredTHSTotalAmount(accLoanYearTotalMs4.getTotalAmount());
            rptAccLoanYearTotalTempVO.setOneHundredAndFiftyTHSTotalAmount(accLoanYearTotalMs5.getTotalAmount());
            rptAccLoanYearTotalTempVO.setTwoHundredTHSTotalAmount(accLoanYearTotalMs6.getTotalAmount());
            rptAccLoanYearTotalTempVO.setTwoHundredAndFiftyTHSTotalAmount(accLoanYearTotalMs7.getTotalAmount());
            rptAccLoanYearTotalTempVO.setThreeHundredTHSTotalAmount(accLoanYearTotalMs8.getTotalAmount());
            rptAccLoanYearTotalTempVO.setFiveTHSCountCus(accLoanYearTotalMs1.getCountCus());
            rptAccLoanYearTotalTempVO.setTenTHSCountCus(accLoanYearTotalMs2.getCountCus());
            rptAccLoanYearTotalTempVO.setFiftyTHSCountCus(accLoanYearTotalMs3.getCountCus());
            rptAccLoanYearTotalTempVO.setOneHundredTHSCountCus(accLoanYearTotalMs4.getCountCus());
            rptAccLoanYearTotalTempVO.setOneHundredAndFiftyTHSCountCus(accLoanYearTotalMs5.getCountCus());
            rptAccLoanYearTotalTempVO.setTwoHundredTHSCountCus(accLoanYearTotalMs6.getCountCus());
            rptAccLoanYearTotalTempVO.setTwoHundredAndFiftyTHSCountCus(accLoanYearTotalMs7.getCountCus());
            rptAccLoanYearTotalTempVO.setThreeHundredTHSCountCus(accLoanYearTotalMs8.getCountCus());
            //将旧数据删除
            int num2 = rptAccLoanYearTotalService.deletByDateAndChannelCode(channelCodeMs);
            //将加工完成的数据入库到 rpt_acc_loan_year_total_temp 表中
            int num = rptAccLoanYearTotalService.insert(rptAccLoanYearTotalTempVO);
            logger.info("======>统计用户当年累计放款额度(百度)开始");
            //1.2单户借据用信不大于5000的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd1 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(startTotalAmount, fiveTHSTotalAmount);
            //2.2单户借据用信大于5000小于1万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd2 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(fiveTHSTotalAmount, tenTHSTotalAmount);
            //3.2单户借据用信大于1万小于5万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd3 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(tenTHSTotalAmount, fiftyTHSTotalAmount);
            //4.2单户借据用信大于5万小于10万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd4 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(fiftyTHSTotalAmount, oneHundredTHSTotalAmount);
            //5.2单户借据用信大于10万小于15万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd5 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(oneHundredTHSTotalAmount, oneHundredAndFiftyTHSTotalAmount);
            //6.2单户借据用信大于15万小于20万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd6 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(oneHundredAndFiftyTHSTotalAmount, twoHundredTHSTotalAmount);
            //7.2单户借据用信大于20万小于25万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd7 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(twoHundredTHSTotalAmount, twoHundredAndFiftyTHSTotalAmount);
            //8.2单户借据用信大于25万小于30万的用户当年累计放款额度
            AccLoanYearTotalTempVo accLoanYearTotalBd8 = accLoanYearTotalTempService.getAccLoanYearTotalForBd(twoHundredAndFiftyTHSTotalAmount, threeHundredTHSTotalAmount);
            logger.info("======>统计用户当年累计放款额度(百度)结束");
            RptAccLoanYearTotalTempVO rptAccLoanYearTotalTempVO1 = new RptAccLoanYearTotalTempVO();
            rptAccLoanYearTotalTempVO1.setChannelCode(channelCodeBd);
            rptAccLoanYearTotalTempVO1.setChannelName(channelMameBd);
            rptAccLoanYearTotalTempVO1.setFiveTHSTotalAmount(accLoanYearTotalBd1.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setTenTHSTotalAmount(accLoanYearTotalBd2.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setFiftyTHSTotalAmount(accLoanYearTotalBd3.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setOneHundredTHSTotalAmount(accLoanYearTotalBd4.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setOneHundredAndFiftyTHSTotalAmount(accLoanYearTotalBd5.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setTwoHundredTHSTotalAmount(accLoanYearTotalBd6.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setTwoHundredAndFiftyTHSTotalAmount(accLoanYearTotalBd7.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setThreeHundredTHSTotalAmount(accLoanYearTotalBd8.getTotalAmount());
            rptAccLoanYearTotalTempVO1.setFiveTHSCountCus(accLoanYearTotalBd1.getCountCus());
            rptAccLoanYearTotalTempVO1.setTenTHSCountCus(accLoanYearTotalBd2.getCountCus());
            rptAccLoanYearTotalTempVO1.setFiftyTHSCountCus(accLoanYearTotalBd3.getCountCus());
            rptAccLoanYearTotalTempVO1.setOneHundredTHSCountCus(accLoanYearTotalBd4.getCountCus());
            rptAccLoanYearTotalTempVO1.setOneHundredAndFiftyTHSCountCus(accLoanYearTotalBd5.getCountCus());
            rptAccLoanYearTotalTempVO1.setTwoHundredTHSCountCus(accLoanYearTotalBd6.getCountCus());
            rptAccLoanYearTotalTempVO1.setTwoHundredAndFiftyTHSCountCus(accLoanYearTotalBd7.getCountCus());
            rptAccLoanYearTotalTempVO1.setThreeHundredTHSCountCus(accLoanYearTotalBd8.getCountCus());
            //将旧数据删除
            int num3 = rptAccLoanYearTotalService.deletByDateAndChannelCode(channelCodeBd);
            //将加工完成的数据入库到 rpt_acc_loan_year_total_temp 表中
            int num1 = rptAccLoanYearTotalService.insert(rptAccLoanYearTotalTempVO1);
            if (num == -1 || num1 == -1 || num2 == -1 || num3 == -1) {
                result = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            String remark = "加工数据入库- rpt_acc_loan_year_total_temp" + e.getMessage();
            logger.error(remark);
            result = false;
        }
        return result;
    }
}
